import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from "@/page/index/Index.vue";
import {Toast} from "vant";
import store from "@/store";


Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'index',
        component: Index
    },
    {
        path: '/index',
        redirect: { name: 'index' }
    },
    {
        path: '/type',
        name: 'type',
        component: () => import('@/page/type/Type.vue')
    },
    {
        path: '/my',
        name: 'my',
        component: () => import('@/page/my/home.vue')
    },
    {
        path: '/cart',
        name: 'cart',
        component: () => import('@/page/cart/cart.vue')
    },
    {
        path: '/public',
        component: () => import('@/page/public/index.vue'),
        children: [
            {
                path: 'login',
                name: 'login',
                component: () => import('@/page/public/login.vue')
            },
            {
                path: 'register',
                name: 'register',
                component: () => import('@/page/public/register.vue')
            },
        ]
    },
    {
        path: '/pages',
        component: () => import('@/page/pages/pageBase.vue'),
        children: [
            {
                path: 'brand/list',
                name: 'brandList',
                meta: {
                    title: '推荐商品列表'
                },
                component: () => import('@/page/pages/brand/list.vue')
            },
            {
                path: 'brand/Detail',
                name: 'brandDetail',
                meta: {
                    title: '品牌详情'
                },
                component: () => import('@/page/pages/brand/detail.vue')
            },
            {
                path: 'product/Detail',
                name: 'productDetail',
                meta: {
                    showHeader: false,
                },
                component: () => import('@/page/pages/product/detail.vue')
            },
            {
                path: 'address/List',
                name: 'addressList',
                meta: {
                    title: '收货地址',
                    login: true,
                },
                component: () => import('@/page/pages/address/list.vue'),
            },
            {
                path: 'address/Manage',
                name: 'addressManage',
                meta: {
                    login: true,
                    showHeader: false,
                },
                component: () => import('@/page/pages/address/manage.vue')
            },
            {
                path: 'history/History',
                name: 'myHistory',
                meta: {
                    login: true,
                    title: '我的足迹'
                },
                component: () => import('@/page/pages/history/history.vue')
            },
            {
                path: 'care/List',
                name: 'careList',
                meta: {
                    login: true,
                    title: '我的关注'
                },
                component: () => import('@/page/pages/care/list.vue')
            },
            {
                path: 'collection/Collection',
                name: 'myCollection',
                meta: {
                    login: true,
                    title: '我的收藏'
                },
                component: () => import('@/page/pages/collection/collection.vue')
            },
            {
                path: 'order/Order',
                name: 'orderOrder',
                meta: {
                    login: true,
                    title: '我的订单'
                },
                component: () => import('@/page/pages/order/order.vue')
            },
            {
                path: 'order/createOrder',
                name: 'createOrder',
                meta: {
                    login: true,
                    title: '创建订单',
                },
                component: () => import('@/page/pages/order/create.vue')
            },
            {
                path: 'notice/Notice',
                name: 'noticeNotice',
                meta: {
                    title: '通知',
                    login: true,
                },
                component: () => import('@/page/pages/notice/notice.vue')
            },
            {
                path: 'set/Set',
                name: 'setSet',
                meta: {
                    title: '设置',
                    login: true,
                },
                component: () => import('@/page/pages/set/set.vue')
            }
        ],
    }
]

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return {
                x: 0,
                y: 0
            }
        }
    }
})

router.beforeEach((to,from,next) => {
    if ('login' in to.meta && to.meta.login) {
        if (!store.state.userInfo.token) {
            console.log('asdqwe')
            Toast({
                message: '需要登录才能访问哦~',
                forbidClick: true,
                type: "fail"
            })
            setTimeout(() => {
                router.push({name: 'login'})
            },500)
            return
        }
    }
    next()
})

export default router
